package com.smartlifev30.baiwei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.smartlifev30.R;
import com.smartlifev30.mvp.presenter.BasePresenter;
import com.smartlifev30.utils.Glide;

import butterknife.BindView;

import butterknife.ButterKnife;
/**
 * @author tys
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener {

    private int imgs[] = {R.drawable.start_1, R.drawable.start_2, R.drawable.start_3, R.drawable.start_4};

    @BindView(R.id.vp)
    ViewPager viewPager;
    @BindView(R.id.point_ll)
    LinearLayout pointLl;
    private int current;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        initView();

    }

    private void initView() {
        viewPager.setAdapter(new Adapter());
        for (int i = 0, size = imgs.length; i < size; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setId(i);
            imageView.setPadding(8, 0, 8, 0);
            if (i != 0) {
                imageView.setImageResource(R.drawable.guide_point_nor);
            } else {
                imageView.setImageResource(R.drawable.guide_point_selected);
            }
            pointLl.addView(imageView);
        }
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                ImageView last = pointLl.findViewById(current);
                last.setImageResource(R.drawable.guide_point_nor);
                ImageView next = pointLl.findViewById(position);
                next.setImageResource(R.drawable.guide_point_selected);
                current = position;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.guide_page_bt:
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
                default:
        }
    }

    private class Adapter extends PagerAdapter {
        @Override
        public int getCount() {
            return imgs.length;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = LayoutInflater.from(GuideActivity.this).inflate(R.layout.guid_page, null);
            Button bt = view.findViewById(R.id.guide_page_bt);
            ImageView iv = view.findViewById(R.id.guide_page_iv);
            if (position == imgs.length - 1) {
                bt.setVisibility(View.VISIBLE);
                bt.setOnClickListener(GuideActivity.this);
            } else {
                bt.setVisibility(View.GONE);
            }
            container.addView(view);
            Glide.with(GuideActivity.this)
                    .load(imgs[position])
                    .fitCenter()
                    .into(iv);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

    @Override
    protected BasePresenter createPresent() {
        return null;
    }
}
